package gait.cannon.dao;

import gait.cannon.bean.User;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

@Repository
public class UserDao implements IUserDao {

	private JdbcTemplate jdbcTemplate;
	
	@Resource
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	public void delete(Integer id) {
		String sql = "delete from user where id=?";
		Object[] args = new Object[] { id };
		jdbcTemplate.update(sql, args);
	}

	public void get(Integer id) {
		
	}

	public void insert(User user) {
		String sql = "insert into user values (null, ?, ?)";
		Object[] args = new Object[] { user.getUsername(), user.getPassword() };
		jdbcTemplate.update(sql, args);
	}

	public List<User> list() {
		String sql = "select * from user";
		return jdbcTemplate.query(sql, new RowMapper<User>() {
			public User mapRow(ResultSet rs, int arg) throws SQLException {
				User user = new User();
				user.setId(rs.getInt("id"));
				user.setUsername(rs.getString("username"));
				user.setPassword(rs.getString("password"));
				return user;
			}
		});
	}

	public void update(User user) {
		
	}

}
